package com.eastfair.transport.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.venuebooking.api.ServiceOrderFeign;
import com.eastfair.venueservice.dto.ServiceOrderDTO;
import com.eastfair.venueservice.entity.ServiceOrder;
import com.eastfair.venueservice.vo.ServiceOrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * <p>
 * 前端控制器
 * 服务订单
 * </p>
 *
 * @author dq
 * @date 2022-09-28
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/serviceOrder")
@Api(value = "ServiceOrder", tags = "服务订单")
public class ServiceOrderController {

    @Resource
    private ServiceOrderFeign serviceOrderFeign;

    /**
     * 分页查询服务订单列表.
     *
     * @param pageParams the page params
     * @return the r
     */
    @ApiOperation("分页查询服务订单列表")
    @PostMapping("/query/serviceOrder")
    public R<Page<ServiceOrderVO>> queryServiceOrders(@RequestBody PageParams<ServiceOrderDTO> pageParams) {
        log.info("queryServiceOrders - 分页查询服务订单列表, pageParams={}", pageParams);
        try {
            return serviceOrderFeign.queryServiceOrders(pageParams);
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 订单编号获取服务订单VO.
     *
     * @param orderNumber the order number
     * @return the by id
     */
    @ApiOperation("订单编号获取服务订单VO")
    @GetMapping("/get/{orderNumber}")
    public R<ServiceOrderVO> getById(@PathVariable("orderNumber") String orderNumber) {
        log.info("getById - 订单编号获取服务订单VO, orderNumber={}", orderNumber);
        try {
            return serviceOrderFeign.getById(orderNumber);
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 保存订单.
     *
     * @param serviceOrderDTO the service order dto
     * @return the r
     */
    @ApiOperation("保存服务订单")
    @SysLog("添加服务订单")
    @PostMapping("/save")
    public R<ServiceOrder> saveServiceOrder(@RequestBody ServiceOrderDTO serviceOrderDTO) {
        log.info("saveServiceOrder - 保存服务订单, serviceOrderDTO={}", serviceOrderDTO);
        try {
            return serviceOrderFeign.saveServiceOrder(serviceOrderDTO);
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 修改服务订单.
     *
     * @param serviceOrderDTO the service order dto
     * @return the r
     */
    @ApiOperation("修改服务订单")
    @SysLog("修改服务订单")
    @PostMapping("/update")
    public R<ServiceOrder> updateServiceOrder(@RequestBody ServiceOrderDTO serviceOrderDTO) {
        log.info("updateServiceOrder - 修改服务订单, serviceOrderDTO={}", serviceOrderDTO);
        try {
            return serviceOrderFeign.updateServiceOrder(serviceOrderDTO);
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 提交订单.
     *
     * @param orderNumber the order number
     * @return the r
     */
    @ApiOperation("提交订单")
    @SysLog("提交服务订单")
    @GetMapping("/submit/{orderNumber}")
    public R<Boolean> submit(@PathVariable("orderNumber") String orderNumber) {
        log.info("submit - 提交订单, orderNumber={}", orderNumber);
        try {
            return serviceOrderFeign.submit(orderNumber);
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 取消订单.
     *
     * @param orderNumber the order number
     * @return the r
     */
    @ApiOperation("取消订单")
    @SysLog("取消服务订单")
    @GetMapping("/cancel/{orderNumber}")
    public R<Boolean> cancel(@PathVariable("orderNumber") String orderNumber) {
        log.info("cancel - 取消订单, orderNumber={}", orderNumber);
        try {
            return serviceOrderFeign.cancel(orderNumber);
        } catch (BizException e) {
            return R.fail(e);
        }
    }

}
